package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcOldManSupplementMapper;
import com.csun.cmny.provider.dao.OmcUserTypeMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.OldmanTypeConstant;
import com.csun.cmny.provider.model.domain.OmcOldManSupplement;
import com.csun.cmny.provider.model.domain.OmcUserType;
import com.csun.cmny.provider.model.dto.OmcUserTypeDto;
import com.csun.cmny.provider.model.dto.OmcUserTypeQueryDto;
import com.csun.cmny.provider.model.enums.OmcUserTypeCodeEnum;
import com.csun.cmny.provider.service.OmcUserTypeService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author chenzheng
 * @description:
 * @date 2019/8/10 11:52
 */
@Slf4j
@Service
public class OmcUserTypeServiceImpl extends BaseService<OmcUserType> implements OmcUserTypeService {
    @Resource
    private OmcUserTypeMapper omcUserTypeMapper;

    @Resource
    private OmcOldManSupplementMapper oldManSupplementMapper;

    /**
     * 保存或者更新人员类别
     *
     * @param omcUserType
     * @param loginAuthDto
     * @return
     */
    @Override
    public int saveOmcUserType(OmcUserType omcUserType, LoginAuthDto loginAuthDto) {
        omcUserType.setUpdateInfo(loginAuthDto);
        if (omcUserType.isNew()) {//保存
            omcUserType.setUserTypeCode("0");
            return omcUserTypeMapper.insertSelective(omcUserType);
        } else {//更新
            OmcUserType userType = selectByKey(omcUserType.getId());
            if(userType == null){
                throw new OmcBizException(ErrorCodeEnum.OMC10039001);
            }
            if(!userType.getUserTypeCode().equals("0")){
                throw new OmcBizException(ErrorCodeEnum.OMC10039005);
            }
            //人员类别是否被使用
            OmcOldManSupplement oldManSupplement = new OmcOldManSupplement();
            oldManSupplement.setPeopleType(userType.getId());
            if (oldManSupplementMapper.selectCount(oldManSupplement) > 0) {
                throw new OmcBizException(ErrorCodeEnum.OMC10039004);
            }
            return omcUserTypeMapper.updateByPrimaryKeySelective(omcUserType);
        }
    }

    /**
     * 根据人员类别名模糊查询人员类别列表
     *
     * @param omcUserTypeQueryDto
     * @return
     */
    @Override
    public List<OmcUserType> selectByName(OmcUserTypeQueryDto omcUserTypeQueryDto) {
        return omcUserTypeMapper.selectByName(omcUserTypeQueryDto);
    }

    /**
     * 删除人员类别
     *
     * @param userTypeId
     * @return
     */
    @Override
    public int deleteOmcUserType(Long userTypeId) {
        OmcUserType userType = selectByKey(userTypeId);
        if(userType == null){
            throw new OmcBizException(ErrorCodeEnum.OMC10039001);
        }
        if(!userType.getUserTypeCode().equals("0")){
            throw new OmcBizException(ErrorCodeEnum.OMC10039003);
        }
        //人员类别是否被使用
        OmcOldManSupplement oldManSupplement = new OmcOldManSupplement();
        oldManSupplement.setPeopleType(userType.getId());
        if (oldManSupplementMapper.selectCount(oldManSupplement) > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10039002);
        }
        return deleteByKey(userTypeId);
    }

    /**
     * 根据人员类别名模糊查询人员类别列表
     *
     * @param userTypeDto
     * @return
     */
    @Override
    public int isNameExist(OmcUserTypeDto userTypeDto) {
        return omcUserTypeMapper.selectNameExist(userTypeDto);
    }

    @Override
    public Integer addBasePeopleTypes(Long groupId, LoginAuthDto loginAuthDto) {

        if(groupId == null || groupId.equals(0)){
            groupId = loginAuthDto.getGroupId();
        }

        List<OmcUserType> userTypesList = new LinkedList<>();
        OmcUserType userTypeFir = new OmcUserType("政府供养特困对象", OldmanTypeConstant.GOVERNMENT_SUPPORT_POOR_PEOPLE,groupId);
        if(checkNameExist(userTypeFir) == 0){
            userTypesList.add(userTypeFir);
        }

        OmcUserType userTypeSec = new OmcUserType("困境家庭保障对象", OldmanTypeConstant.POOR_FAMILY_PROTECT_PEOPLE,groupId);
        if(checkNameExist(userTypeSec) == 0){
            userTypesList.add(userTypeSec);
        }

        OmcUserType userTypeThr = new OmcUserType("优待家庭保障对象", OldmanTypeConstant.TREATMENT_FAMILY_SUPPORT_PEOPLE,groupId);
        if(checkNameExist(userTypeThr) == 0){
            userTypesList.add(userTypeThr);
        }

        OmcUserType userTypeFou = new OmcUserType("失独老人", OldmanTypeConstant.NO_FAMILY_SUPPORT_PEOPLE,groupId);
        if(checkNameExist(userTypeFou) == 0){
            userTypesList.add(userTypeFou);
        }

        OmcUserType userTypeFif = new OmcUserType("社会代养", OldmanTypeConstant.SOCIETY_SUPPORT_PEOPLE,groupId);
        if(checkNameExist(userTypeFif) == 0){
            userTypesList.add(userTypeFif);
        }

        OmcUserType userTypeSix = new OmcUserType("其他类别老人", OldmanTypeConstant.OHTER_TYPE_PEOPLE,groupId);
        if(checkNameExist(userTypeSix) == 0){
            userTypesList.add(userTypeSix);
        }

        return batchSave(userTypesList);
    }

    public int checkNameExist(OmcUserType omcUserType){
        Example example = new Example(OmcUserType.class);
        Example.Criteria criteria = example.createCriteria();
        if(omcUserType.getId()!= null && !omcUserType.getId().equals(0)){
            criteria.andNotEqualTo("id",omcUserType.getId());
        }
        criteria.andEqualTo("userTypeName",omcUserType.getUserTypeName());
        criteria.andEqualTo("groupId",omcUserType.getGroupId());
        return selectCountByExample(example);
    }
}
